package com.mobilepay.moneypig.model.activity.home;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mobilepay.moneypig.R;
import com.mobilepay.moneypig.base.BaseActivity;
import com.mobilepay.moneypig.base.NoDoubleClickListener;
import com.mobilepay.moneypig.model.application.App;
import com.mobilepay.moneypig.model.dialog.TipDialog;
import com.mobilepay.moneypig.utils.CommonUtil;
import com.mobilepay.moneypig.utils.QRCodeUtil;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

/**
 * Created by xlm on 2017/8/2 0002
 */

public class PayCodeActivity extends BaseActivity {
    private TextView mTextTitle;
    private RelativeLayout mRelativeBack;
    private ImageView mImageCode;
    private TextView mTextMoney;
    private TextView mTextSao;
    private ImageView mImageAvatar;
    private RelativeLayout mRelativeShare;
    private RelativeLayout mRelativePay;
    private TextView mTextType;
    private String type;
    private String amount;
    private String code_url;

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_pay_code);
    }

    public void finishPayCodeActivity() {
        finish();
    }

    @Override
    protected void initViews() {
        mRelativeBack = (RelativeLayout) findViewById(R.id.paycode_back);
        mTextTitle = (TextView) findViewById(R.id.paycode_type);
        mImageCode = (ImageView) findViewById(R.id.paycode_image);
        mImageAvatar = (ImageView) findViewById(R.id.paycode_avatar);
        mTextMoney = (TextView) findViewById(R.id.paycode_num);
        mRelativeShare = (RelativeLayout) findViewById(R.id.paycode_share);
        mRelativePay = (RelativeLayout) findViewById(R.id.paycode_pay);
        mTextSao = (TextView) findViewById(R.id.paycode_sao);
        mTextType = (TextView) findViewById(R.id.pay_type);
    }

    @Override
    protected void onGetIntent(Intent intent) {
        type = intent.getStringExtra("type");
        amount = intent.getStringExtra("amount");
        code_url = intent.getStringExtra("code_url");
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        App.getApp().payCodeActivity = this;
        initView();
//        if (Build.VERSION.SDK_INT >= 23) {
//            String[] mPermissionList = new String[]{
//                    Manifest.permission.ACCESS_FINE_LOCATION,
//                    Manifest.permission.CALL_PHONE, Manifest.permission.READ_LOGS,
//                    Manifest.permission.READ_PHONE_STATE,
//                    Manifest.permission.READ_EXTERNAL_STORAGE,
//                    Manifest.permission.SET_DEBUG_APP,
//                    Manifest.permission.SYSTEM_ALERT_WINDOW,
//                    Manifest.permission.GET_ACCOUNTS, Manifest.permission.WRITE_APN_SETTINGS};
//            ActivityCompat.requestPermissions(this, mPermissionList, 123);
//        }
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                isCut = CommonUtil.getAndSaveCurrentImage(getThis());
//            }
//        }).start();
    }

    private Bitmap bitmap;

    private void initView() {
        if ("alipay".endsWith(type)) {
            mTextTitle.setText("支付宝支付");
            mTextSao.setText("请使用支付宝扫一扫付款");
            mTextType.setText("支付宝");
            mImageAvatar.setImageResource(R.mipmap.paycode_alipy);
        } else if ("weixin".endsWith(type)) {
            mTextTitle.setText("微信支付");
            mTextSao.setText("请使用微信扫一扫付款");
            mTextType.setText("微信");
            mImageAvatar.setImageResource(R.mipmap.paycode_weixin);
        }
        mTextMoney.setText("￥" + amount + "元");
        bitmap = QRCodeUtil.createQRImage(code_url);
        mImageCode.setImageBitmap(bitmap);
    }

    @Override
    protected void onSetListener() {
        mRelativeBack.setOnClickListener(this);
        mRelativeShare.setOnClickListener(this);
        mRelativePay.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                super.onNoDoubleClick(view);
                if (CommonUtil.getAndSaveCurrentImage(getThis())) {
                    if ("alipay".endsWith(type)) {
                        showToastLong("二维码已截图");
                    }
                    showTipDialog(type);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.paycode_back:
                finish();
                break;
            case R.id.paycode_share:
                if ("alipay".endsWith(type)) {
                    new ShareAction(getThis())
                            .withMedia(new UMWeb(code_url, "支付宝付款", "请点击链接或扫二维码码支付", new UMImage(getThis(), bitmap)))
                            .setDisplayList(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.ALIPAY)
                            .open(App.setShareBoard());
                } else if ("weixin".endsWith(type)) {
                    new ShareAction(getThis())
                            .withText(code_url)
                            .setDisplayList(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.ALIPAY)
                            .open(App.setShareBoard());
                }
                break;
        }
    }

    private TipDialog tipDialog;

    private void showTipDialog(final String type) {
        if (tipDialog == null) {
            tipDialog = new TipDialog(this);
        }
//        打开 支付宝/微信点“扫一扫”从相册导入刚保存的二维码图片进行支付
        if ("alipay".endsWith(type)) {
            tipDialog.setContent("  打开支付宝点“扫一扫”,从相册导入刚保存的二维码图片进行支付");
        } else {
            ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            // 将文本内容放到系统剪贴板里。
            cm.setText(code_url);
            tipDialog.setContent("  支付链接复制成功,请打开微信粘贴,点击链接支付");
        }
        tipDialog.setCancelable(true);
        tipDialog.show();
        tipDialog.setListener(new TipDialog.Listener() {
            @Override
            public void onConfirm() {
                super.onConfirm();
                tipDialog.dismiss();
                if ("alipay".endsWith(type)) {
                    Uri uri = Uri.parse("alipayqr://platformapi/startapp?saId=10000007");
                    Intent action = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(action);
                } else if ("weixin".endsWith(type)) {
                    Uri uri = Uri.parse("weixin://");
                    Intent action = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(action);
                }
            }

            @Override
            public void onDismiss() {
                super.onDismiss();
                tipDialog.dismiss();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        UMShareAPI.get(this).release();
    }
}
